﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Sauces.Bussinees.Entities.Almacen;
using Sauces.Bussinees.Logic.Service;
using Microsoft.Practices.Unity;
//using Microsoft.Practices.Unity.Configuration;
using System.Web.ModelBinding;




namespace Sauces.Web.Almacen
{
    public partial class FrmCategoria : System.Web.UI.Page
    {
        [Dependency]
        public ICategoriaService CategoriaService { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {
            var action = Request.QueryString["action"];
            var IdCategoria = Request.QueryString["IdCategoria"];

            if (action != null && action == "new")
            {
                fvCategoria.DefaultMode = FormViewMode.Insert;
            }


            else if (action != null && IdCategoria != null && action == "delete")
            {
                var id = Int32.Parse(IdCategoria.ToString());

                CategoriaService.removeCategoria(id);
                Response.Redirect("FrmListaCategoria.aspx");
            }

        }

        public Categoria GetCategoria([QueryString("IdCategoria")]Int32? id)
        {
            if (id.HasValue)
            {
                return CategoriaService.getFromCategoriaById(id.Value);
            }
            return null;
        }

        public void AddCategoria(Categoria categoria)
        {
            if (ModelState.IsValid)
            {
                CategoriaService.addCategoria(categoria);
                Response.Redirect("FrmListaCategoria.aspx");
            }
        }

        public void UpDateCategoria(Categoria categoria)
        {
            if(ModelState.IsValid)
            {
                CategoriaService.upDateCategoria(categoria);
                Response.Redirect("FrmListaCategoria.aspx");
            }
        }
      }
 }
